package com.kystar.muxplayer.ui;

import android.content.Context;
import android.graphics.Rect;
import android.util.Size;
import android.widget.EditText;
import android.widget.TextView;

import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.model.Source;
import com.kystar.muxplayer.selfview.MyEditText;
import com.kystar.muxplayer.selfview.MyKeyBoard;

/**
 * Created by zhusheng on 2018-03-10.
 */
public class InputGroupView extends GroupView {
    private ColorSeekBar contrastSeekBar;
    private ViewList edidViewList;
    private ViewList edidHzViewList;
    private Source source;
    private RectGroupView vgaView;
    private OkCancelView edidFormButtons;
    private OkCancelView modeFormButtons;
    private OkCancelView vgaFormButtons;
    private MyEditText edidWidth;
    private MyEditText edidHeight;
    private MyEditText modeWidth;
    private MyEditText modeHeight;
    private TextView edidSourceName;
    private TextView modeSourceName;
    MyKeyBoard mMyKeyBoard;

    public InputGroupView(Context context,MyKeyBoard myKeyBoard) {
        super(context, "信号设置");
        mMyKeyBoard = myKeyBoard;
        addSubGroup("输入对比度", R.layout.layout_main_settings_signal_contrast, false);
        addSubGroup("EDID设置", R.layout.layout_main_settings_signal_edid, false);
        addSubGroup("信号有效模式", R.layout.layout_main_settings_signal_mode, false);
        addSubGroup("VGA矫正", R.layout.layout_main_settings_signal_vga, true);

        bindView();
    }

    public void bindView() {
        bindContrastView();
        bindEdidView();
        bindResolutionView();
        bindVgaView();

        edidViewList.select(1, true);
    }

    private void bindContrastView() {
        contrastSeekBar = mContentView.findViewById(R.id.csb_signal_contrast);
        contrastSeekBar.setMax(255);
        contrastSeekBar.setOnChangListener(value -> {
//            if (value != source.contrast) {
//                connection.run(p -> p.setSourceContrast(source.getId(), value))
//                        .onOK(()->source.contrast = value)
//                        .post();
//            }
        });
    }

    private void bindEdidView() {
        edidSourceName = findViewById(R.id.signal_name_in_edid);

        edidViewList = new ViewList(mContentView, R.id.signal_edid_buttons, true);
        edidViewList.setOnClickListener(v -> {
            if ("default".equals(Helper.getTag(v))) {
                Helper.showView(Helper.getChildView(edidViewList.get(0), 1));
                Helper.hideView(Helper.getChildView(edidViewList.get(1), 1));
                Helper.hideView(mContentView.findViewById(R.id.signal_edid_manual));
                Helper.showView(mContentView.findViewById(R.id.signal_edid_default));
            } else {
                Helper.hideView(Helper.getChildView(edidViewList.get(0), 1));
                Helper.showView(Helper.getChildView(edidViewList.get(1), 1));
                Helper.hideView(mContentView.findViewById(R.id.signal_edid_default));
                Helper.showView(mContentView.findViewById(R.id.signal_edid_manual));
            }
        });
        edidWidth = mContentView.findViewById(R.id.signal_edid_width);
        edidHeight = mContentView.findViewById(R.id.signal_edid_height);
        edidWidth.setOnClickListener(v->{
            mMyKeyBoard.show((MyEditText) v);
        });
        edidHeight.setOnClickListener(v->{
            mMyKeyBoard.show((MyEditText) v);
        });

        edidFormButtons = new OkCancelView(mContentView.findViewById(R.id.signal_edit_form_buttons));
        edidHzViewList = new ViewList(mContentView, R.id.signal_edid_hz_buttons, false);
        edidHzViewList.setOnClickListener(v -> edidFormButtons.show());

        edidFormButtons.watch(true, edidWidth, edidHeight).onOk(()->{
            try {
                Size size = Helper.parseSize(edidWidth, edidHeight);
                int hzIndex = Helper.getTagInt(edidHzViewList.selected(),0);
//                connection.run(p-> p.setSourceEdidCustom(source.getId(), size, hzIndex))
//                        .onOK(()->{
//                            source.edidSize = size;
//                            source.edidFreq = FreqType.valueOf(hzIndex);
//                        })
//                        .post();
            } catch (Exception ex) {
                Helper.show(ex.getMessage());
            }
        }).onCancel(()->{
//            edidWidth.setText(String.valueOf(source.edidSize.getWidth()));
//            edidHeight.setText(String.valueOf(source.edidSize.getHeight()));
//            edidHzViewList.select(String.valueOf(source.edidFreq.getId()), false);
        }).hide();

        setupConfirm(mContentView.findViewById(R.id.signal_edid_default),
                "是否恢复默认设置?",
                () -> {
//                    connection.run(p -> {
//                        p.setSourceEdidDefault(source.getId());
//                        return p.getSource(source.getId());
//                    })
//                    .post();
                },
                () -> edidViewList.select(1, true));
    }

    private void bindResolutionView() {
        modeSourceName = findViewById(R.id.signal_name_in_mode);

        ViewList validModeBtns = new ViewList(mContentView, R.id.signal_resolution_mode_buttons, true);
        validModeBtns.setOnClickListener(v -> {
            if ("kommander".equals(Helper.getTag(v))) {
//                connection.run(p-> p.setSourceValidMode(source.getId(), SourceValidType.Kommand, null))
//                        .onOK(()-> source.validType = SourceValidType.Kommand)
//                        .post();
            }
        });

        ViewList validModeCustom = new ViewList(mContentView, R.id.signal_resolution_custom_buttons, true);
        validModeCustom.setOnClickListener(v -> {
            if("default".equals(Helper.getTag(v))) {
//                connection.run(p -> p.setSourceValidMode(source.getId(), SourceValidType.Auto, null))
//                        .onOK(()->source.validType = SourceValidType.Auto)
//                        .post();
            }
        });

        modeWidth = mContentView.findViewById(R.id.signal_mode_width);
        modeHeight = mContentView.findViewById(R.id.signal_mode_height);
        modeWidth.setOnClickListener(v -> mMyKeyBoard.show((MyEditText) v));
        modeHeight.setOnClickListener(v -> mMyKeyBoard.show((MyEditText) v));
        modeFormButtons = new OkCancelView(mContentView.findViewById(R.id.signal_mode_forms));
//        modeFormButtons.watch(true, modeWidth, modeHeight).onOk(()->{
//            try {
//                Size size = Helper.parseSize(modeWidth, modeHeight);
////                connection.run(p-> p.setSourceValidMode(source.getId(), SourceValidType.Custom, size))
////                        .onOK(()-> source.validSize = size)
////                        .post();
//            } catch (Exception ex) {
//                Helper.show(ex.getMessage());
//            }
//        }).onCancel(()->{
////            modeWidth.setText(String.valueOf(source.validSize.getWidth()));
////            modeHeight.setText(String.valueOf(source.validSize.getHeight()));
//        }).hide();
    }

    private void bindVgaView() {
        vgaView = new RectGroupView(mContentView.findViewById(R.id.input_vga_rect),mMyKeyBoard);
        vgaFormButtons = new OkCancelView(mContentView.findViewById(R.id.signal_vga_form_buttons));
        vgaFormButtons.watch(vgaView).onOk(()->{
            try {
                Rect rect = vgaView.getRect();
//                connection.run(p-> p.setVGARect(source.source, rect))
//                        .onOK(()->source.vga = rect)
//                        .post();
            } catch (Exception ex) {
                Helper.show(ex.getMessage());
            }
        }).onCancel(()-> {
           // vgaView.setRect(source.vga)
        }).hide();
    }

    public void setSource(Source source) {
        if (source == this.source) {
            return;
        }
        this.source = source;

//        edidSourceName.setText(source.source.getName());
//        modeSourceName.setText(source.source.getName());
//        contrastSeekBar.setValue(source.contrast);
        edidFormButtons.cancel();
        modeFormButtons.cancel();
        vgaFormButtons.cancel();

//        if (source.isDigitalSignal()) {
//            edidViewList.each(view -> view.setClickable(true));
//            edidViewList.select(1, true);
//            edidHzViewList.each(view -> view.setClickable(true));
//            edidWidth.setEnabled(true);
//            edidHeight.setEnabled(true);
//        } else {
//            edidViewList.select(1, true);
//            edidViewList.clearSelected();
//            edidViewList.each(view -> view.setClickable(false));
//            edidHzViewList.each(view -> view.setClickable(false));
//            edidWidth.setEnabled(false);
//            edidHeight.setEnabled(false);
//        }
    }
}
